import Vue from 'vue'
import Vuex from 'vuex'
import Api from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    overview: {
      namespaced: true,
      state: {
        msg: 'i am overview'
      },
      modules: {
        buttonGroup: {
          namespaced: true,
          state: {
            musicIsPlay: false,
            vedioIsPlay: false,
            musicButtonIsActivety: false,
            vedioButtonIsActivety: false
          }
        },
        main: {
          namespaced: true,
          state: {
            title: '',
            content: '',
            overviewChunk: [],
            contentMap: [
              "余杭概况",
              "旅游资源",
              "美食特产",
              "余杭符号"
            ],
          },
          mutations: {
            loadOverview(state, index) {
              debugger
              state.title = state.contentMap[index];
              state.content = overviewChunk.find(function (item) {
                return item.title === state.title;
              }).content;
            },
            loadOverviewChuck(state){
              debugger
              if (!state.overviewChunk.length) {
                Api.fetchOverview().then(function (res) {
                  state.overviewChunk = res;
                })
              }
            }
          }
        },
        navigation: {
          namespaced: true,
          state: {
            current: 0,

          },
          mutations: {
            updateIndex(state, index) {
              debugger
              state.current = index;
            }
          },
          actions: {
            checkInit({ commit }) {
              debugger
              commit('../main/loadOverviewChuck');
            },
            switchOverview({ commit, dispatch }, clickElementIndex) {
              debugger
              dispatch('checkInit').then(function () {
                debugger
                commit('updateIndex', clickElementIndex);
                commit('../main/loadOverview', clickElementIndex);
              })
            }
          }
        }
      }
    }
  }
})
